package com.dragonfly.shutter.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dragonfly.shutter.beans.Message;
import com.dragonfly.shutter.beans.PagedBean;
import com.dragonfly.shutter.services.AlbumPictureListService;
import com.dragonfly.shutter.utils.KeywordFilter;

/**
 * 相册内的图片列表控制类
 * @author 黄挺
 *
 */
@Controller
@RequestMapping("/albumPictureList.html")
public class AlbumPictureListController {
	@Autowired
	AlbumPictureListService albumPictureListService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView onLoad(HttpSession session, HttpServletRequest request,
			@RequestParam(value="albumID", required=false)String albumIDStr,
			@RequestParam(value="page", required=false)String pageStr){
		Integer userID = (Integer) session.getAttribute("userID");
		Message message = null;
		PagedBean pictures = null;
		
		//空的参数
		if(albumIDStr == null | pageStr == null){
			message = new Message("空的参数", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		//非法的参数
		if(!KeywordFilter.isNumeric(pageStr) ||
				!KeywordFilter.isNumeric(albumIDStr)){
			message = new Message("非法的参数", "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		Integer albumID = Integer.parseInt(albumIDStr);
		Integer page = Integer.parseInt(pageStr);
		
		try {
			pictures = albumPictureListService.doService(userID, albumID, page, request.getRequestURL().toString());
		} catch (Exception e) {
			// TODO 权限错误
			message = new Message(e.getMessage(), "操作失败", "/Dragonfly/");
			return new ModelAndView("message", "message", message);
		}
		
		return new ModelAndView("albumPictureList", "pictures", pictures);
	}
}
